package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsWorkOrder;
import com.zmj.sy.mom.srv.aps.bean.entity.order.OrderBom;
import com.zmj.sy.mom.srv.aps.bean.vo.report.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OrderBomMapper extends SyMapper<OrderBom> {


    @Select(" select b.* " +
            " from tb_order_bom b " +
            " left join tb_order o on b.order_id = o.id  " +
            " ${ew.customSqlSegment}")
    OrderBom selectCurrent(@Param(Constants.WRAPPER) QueryWrapper<OrderBom> query);

    @Select(" select o.* " +
            " from tb_order o " +
            " left join tb_order_bom ob on ob.order_id = o.id " +
            " ${ew.customSqlSegment}")
    List<OrderBom> selectMbdDxf(@Param(Constants.WRAPPER) QueryWrapper<OrderBom> eq);

    @Select("select bom.mater_code, bom.mater_name, bom.part_count, bom.plm_id " +
            "from tb_order_bom bom " +
            "left join tb_aps_work_order awo on bom.pid = awo.bom_id " +
            "left join tb_aps_work_detail awd on awo.id = awd.aps_work_order_id " +
            "${ew.customSqlSegment} ")
    List<OrderBom> findPartCountList(@Param(Constants.WRAPPER) QueryWrapper<OrderBom> query);

    @Select("delete from tb_order_bom where order_id = #{orderId}")
    void deleteReal(@Param("orderId") Integer orderId);

    @Select("delete from tb_order_bom where id = #{id}")
    void deleteByBomId(@Param("id") Integer id);


//    @Select(" select o.serial_code serialCode, o.id orderId, awo.id workOrderId, awo.plm_id plmId, order_count orderCount, awd.id apsWorkDetailId, ao.seq pkgCode, awo.id id " +
//            " from tb_aps_work_order awo " +
//            " left join tb_order o on awo.order_id = o.id " +
//            " left join tb_aps_work_detail awd on awo.id = awd.aps_work_order_id " +
//            " left join tb_aps_order ao on awo.aps_order_id = ao.id " +
////            "where awo.plm_id = '0153-11-0201-03_170' and o.order_type = 1 and o.order_status >= 6 and awo.work_chain <> 'WL'; " +
//            " ${ew.customSqlSegment}")
    @Select(" select  " +
            " o.serial_code , ao.seq ," +
            " awo.plm_id ,  " +
            " pd.texture, pd.thick_ness thickness,  " +
            " pp.replace_texture replaceTexture,  " +
            " part.draw_status drawStatus, " +
            " pp.plan_amount orderCount, pp.plan_finish_amount nestCount, " +
            " awd.plan_start_date_time planStartDateTime, np.id id, awd.id apsWorkDetailId, " +
            " ob.create_username createUserCode, ob.create_date_time createDateTime, " +
            " np.rectangle_area rectalgleArea " +
            " from tb_aps_work_order awo  " +
            " left join tb_order_bom ob on ob.id = awo.bom_id "+
            " left join tb_aps_work_detail awd on awo.id = awd.aps_work_order_id and (awd.phase_code = 'JG' OR awd.phase_code = 'HG') " +
            " left join tb_third_sinocam_pushplan pp on pp.nest_plan_id = awd.id  " +
            " left join tb_third_sinocam_receive_nest_part np on pp.nest_plan_id = np.nest_plan_id " +
            " left join tb_third_sinocam_pushdrawing pd on pd.uni_mater_code = pp.uni_mater_code " +
            " left join tb_aps_order ao on awo.aps_order_id = ao.id  " +
            " left join tb_order o on awo.order_id = o.id  " +
            " left join tb_order_part part on part.plm_id = awo.plm_id " +
            " ${ew.customSqlSegment}")
    Page<ReportProductionPartOrderResVo> productionPartOrder(@Param(Constants.WRAPPER) QueryWrapper<ApsWorkOrder> query, Page<ApsWorkOrder> page);

    @Select("SELECT o.construct_no AS constructCode, o.mater_code AS materCode, o.mater_name AS materName, o.order_qty AS orderCount, ob.texture AS texture, ob.mater_high AS thickness, " +
            "MIN( bps.length ) AS standardPlateLength, MIN( bps.width ) AS standardPlateWidth, MIN( bps.plate_rate ) AS plateRate, " +
            "COALESCE ( SUM( ob.total_count * o.order_qty * ob.mater_weight ), 0 ) AS partWeight, MIN( startTime ) AS deliveryTime " +
            "FROM tb_order_bom ob LEFT JOIN tb_order o ON ob.order_id = o.id LEFT JOIN tb_base_plate_standard bps ON ob.texture = bps.texture AND ob.mater_high = bps.height " +
            "LEFT JOIN ( SELECT MIN( plan_start_date_time ) AS startTime, order_id AS id FROM tb_aps_work_detail awd WHERE awd.phase_code IN ('HG','JG') GROUP BY order_id ) awd ON awd.id = o.id " +
            " ${ew.customSqlSegment} ")
    Page<ReportNestPlateNeedResVo> nestPlateNeed(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<OrderBom> qw);

    @Select("SELECT o.construct_no AS constructCode, ob.texture AS texture, ob.mater_high AS thickness, MIN( bps.length ) AS standardPlateLength, " +
            "MIN( bps.width ) AS standardPlateWidth, MIN( bps.plate_rate ) AS plateRate, COALESCE ( SUM( ob.total_count * o.order_qty * ob.mater_weight ), 0 ) AS partWeight " +
            "FROM tb_order_bom ob LEFT JOIN tb_order o ON ob.order_id = o.id LEFT JOIN tb_base_plate_standard bps ON ob.texture = bps.texture AND ob.mater_high = bps.height " +
            " ${ew.customSqlSegment} ")
    List<ReportNestPlateSumResVo> nestPlateSum(@Param(Constants.WRAPPER) QueryWrapper<OrderBom> qw);

    @Select("SELECT " +
            "  ob.plm_id AS plmId, " +
            "  ob.mater_name AS partName, " +
            "  ( o.order_qty * ob.total_count ) AS partCount, " +
            "  ob.mater_weight AS perWeight, " +
            "  ( o.order_qty * ob.total_count * ob.mater_weight ) AS totalWeight, " +
            "  ob.texture AS texture, " +
            "  ob.mater_high AS thickness, " +
            "  op.length AS partLength, " +
            "  op.width AS partWidth, " +
            "  ob2.plm_id AS assPlmId " +
            "FROM " +
            "  tb_order_bom ob " +
            "  LEFT JOIN tb_order o ON ob.order_id = o.id " +
            "  LEFT JOIN tb_order_part op ON ob.plm_id = op.plm_id " +
            "  LEFT JOIN tb_order_bom ob2 ON ob.pid = ob2.id " +
            " ${ew.customSqlSegment} ")
    Page<ReportNestPartInfoResVo> nestPartInfo(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<OrderBom> qw);

    @Select("SELECT ob.plm_id  " +
            "FROM tb_order_bom ob LEFT JOIN tb_order o ON ob.order_id = o.id " +
            "WHERE o.order_type <> 2 AND ob.mater_type = 'Platepart' AND mater_weight IS NULL AND o.mater_code = #{constructCode} ")
    List<String> findNullWeight(@Param("constructCode") String constructCode);

    @Select("SELECT ob.work_chain AS workChain, o.mater_code AS productCode, ob.mater_code AS materCode, ob.mater_name AS materName, ob.plm_id AS plmId, obp.plm_id AS faPlmId, " +
            "ob.mater_type AS materType, op.thickness AS thickness, op.length AS length, op.width AS width, op.net_weight AS weight, op.texture AS texture, " +
            "NULL AS checkStatus, op.area AS area, ob.erp_code AS erpId, ob.due_date AS deliveryDate, ob.work_order_code AS erpWorkOrderCode, NULL AS designDraw, NULL AS technologyDraw " +
            "FROM tb_order_bom ob LEFT JOIN tb_order o ON ob.order_id = o.id LEFT JOIN tb_order_bom obp ON obp.id = ob.pid LEFT JOIN tb_order_part op ON ob.plm_id = op.plm_id " +
            " ${ew.customSqlSegment} ")
    Page<ReportWorkOrderPublishResVo> getWorkOrderPublish(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<OrderBom> qw);


    @Select("select * from tb_order_bom ${ew.customSqlSegment} ")
    List<OrderBom> listByQuery(@Param(Constants.WRAPPER) QueryWrapper<?> qw);
}
